package wxk.interview.generic;

import java.math.BigDecimal;
import java.util.*;

/**
 *
 * 泛型
 * @author wangxuekai
 * @version 2.2
 * @Date 2017/4/1.
 * @since 2.2
 */
public class Main {

    private static HashMap<String,String> map = new HashMap<String, String>(16);

    public static void main(String[] args){
        map.put("name","stan");
        map.put("age","27");
        map.put("salary","300000.012");

        String name = get("name",String.class);
        Integer age = get("age",Integer.class);
        BigDecimal salary = get("salary",BigDecimal.class);

        System.out.println("My name is "+name+" , i am "+age+" years old , my yearly salary is "+salary+" ");


        Node<String> node = new Node<String>("cs",null);


        System.out.println("node' class is "+node.getClass()+" , and node toString:"+node);

        int i = 2 / 100 ;
        System.out.println(i);
    }

    private static <T> T get(String k,Class<T> clazz){
        String v = map.get(k);

        if (clazz.getName().equalsIgnoreCase(String.class.getName())){
            return clazz.cast(v);
        }else if(clazz.getName().equalsIgnoreCase(Integer.class.getName())){
           return clazz.cast(Integer.valueOf(v));
        }else if (clazz.getName().equalsIgnoreCase(BigDecimal.class.getName())){
            return clazz.cast(BigDecimal.valueOf(Double.valueOf(v)));
        }else {
            return null;
        }
    }

    static class Node<T>{
        T t;
        Node<T> next;
        public Node(T t, Node<T> next) {
            this.t = t;
            this.next = next;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("Node{");
            sb.append("t=").append(t);
            sb.append(", next=").append(next);
            sb.append('}');
            return sb.toString();
        }
    }
}
